﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using V5_EShop._Class.BLL.GZ;
using V5_Common;
using System.Data;
using V5_Model;

namespace V5_EShop.Admin.GZProductUser {
    public partial class Admin_UserClass : System.Web.UI.Page {
        #region 开始
        BLLGZUserClass bllGZProduct = new BLLGZUserClass();
        protected void Page_Load(object sender, EventArgs e) {
            if (!this.IsPostBack) {
                string action = InputHelper.SetString(Request["action"]);
                switch (action) {
                    case "edit":
                        this.MultiViewList.ActiveViewIndex = 1;
                        break;
                    default:
                        this.Bind_DataList();
                        break;
                }
            }
        }

        #region 增删改
        private void Bind_DataList() {
            this.MultiViewList.ActiveViewIndex = 0;
            int RecordCount = 0;
            DataTable dt = bllGZProduct.GetList("*", Pager.PageSize, Pager.CurrentPageIndex, string.Empty, string.Empty, ref RecordCount).Tables[0];
            Pager.RecordCount = RecordCount;
            this.gvList.DataSource = dt;
            this.gvList.DataBind();
        }

        private void Bind_DataEdit(Guid ID) {
            this.MultiViewList.ActiveViewIndex = 1;
            ModelGZUserClass model = new BLLGZUserClass().GetModel(ID);
            this.hfID.Value = model.ID.ToString();

            this.txtParentTypeID.Text = model.ParentTypeID.ToString();
            this.txtCustomTypeName.Text = model.CustomTypeName.ToString();
            this.txtDistCount.Text = model.DistCount.ToString();
            this.btnSave.Text = "保存修改";
        }

        private void Bind_Delete(Guid id) {
            bllGZProduct.Delete(id);
            Bind_DataList();
        }
        #endregion



        protected void Pager_PageChanged(object sender, EventArgs e) {
            this.Bind_DataList();
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            string id = this.hfID.Value;
            string ParentTypeID = this.txtParentTypeID.Text;
            string CustomTypeName = this.txtCustomTypeName.Text;
            string DistCount = this.txtDistCount.Text;

            ModelGZUserClass model = new ModelGZUserClass();
            model.ID = new Guid(id == "0" ? "00000000-0000-0000-0000-000000000000" : id);
            model.ParentTypeID = new Guid(ParentTypeID == "0" ? "00000000-0000-0000-0000-000000000000" : ParentTypeID);
            model.CustomTypeName = CustomTypeName;
            model.DistCount = decimal.Parse(DistCount);
            if (id == "0") {
                model.ID = Guid.NewGuid();
                new BLLGZUserClass().Add(model);
            }
            else {
                model.ID = new Guid(id);
                new BLLGZUserClass().Update(model);
            }
            this.Bind_DataList();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e) {
            Guid id = new Guid(Easp.Str.SetString(e.CommandArgument));
            string eName = Easp.Str.SetString(e.CommandName).ToLower();
            switch (eName) {
                case "go":
                    Bind_DataEdit(id);
                    break;
                case "del":
                    Bind_Delete(id);
                    break;
            }
        }

        #endregion 结束
    }
}